package hot100.logos21To30;

import hot100.datastruct.ListNode;

/**
 * @author logos
 * date 2025/7/12 13:46
 * @version v1.0
 * @Package hot100
 */
public class logos29 {
    /**
     * 29.删除链表的倒数第 N 个结点
     * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/?envType=study-plan-v2&envId=top-100-liked
     */

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode(0, head);
        ListNode left = dummyHead;
        ListNode right = dummyHead;
        while (n-- > 0) {
            right = right.next;
        }
        while (right.next != null) {
            left = left.next;
            right = right.next;
        }
        left.next = left.next.next;
        return dummyHead.next;
    }
}
